import path = require('path')
import { FileUtil } from 'tools-mock'
//@ts-ignore
import _obj from './data'

//转存到的目录
const iconUrl = path.join(__dirname, '../../public/vicon')

/**
 * 将windows下不允许的符号转为-
 * @param fileName
 * @returns
 */
function sanitizeWindowsFileName(fileName: string): string {
    // 使用正则表达式替换所有不允许的字符为 '-'
    return fileName.replace(/[\\/:"'*?<>|]/g, '-')
}

/**
 * 将iconfont插件导出为组件的ts文件丢到one目录中并导入即可生成可配置文件，先执行one再执行all
 */
const init = async () => {
    const _keys = Object.keys(_obj)
    for (let i = 0; i < _keys.length; i++) {
        const key = _keys[i]
        FileUtil.deleteFileOrDirectory(path.join(iconUrl, key))
    }

    for (let i = 0; i < _keys.length; i++) {
        const key = _keys[i]
        const _itemkeys = Object.keys(_obj[key])
        for (let j = 0; j < _itemkeys.length; j++) {
            const jkey = _itemkeys[j]
            const val = _obj[key][jkey]
            //"currentColor\":[0]},\"viewBox\":\"0 0 1024 1024\"
            if (val.fill) {
                if (Object.keys(val.fill).length === 1 && val.fill.currentColor?.length === 1) {
                    val.fill = undefined
                    delete val.fill
                }
            }
            if (val.viewBox == '0 0 1024 1024') {
                val.viewBox = undefined
                delete val.viewBox
            }
            await FileUtil.writeFile(
                path.join(iconUrl, `${key}/${sanitizeWindowsFileName(jkey)}.json`),
                JSON.stringify(val)
            )
        }
    }
}

init()
